package org.ghost.wechat.platform.dao;

import org.ghost.wechat.platform.common.http.annotation.HttpClientEx;
import org.ghost.wechat.platform.common.http.annotation.RequestMappingEx;
import org.ghost.wechat.platform.dto.WeChatErrorRspDTO;
import org.ghost.wechat.platform.dto.wechat.web.WeChatOpenIdRspDTO;
import org.ghost.wechat.platform.dto.wechat.web.WeChatUserInfoRspDTO;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author 01
 * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
 */
@HttpClientEx(hostName = "https://api.weixin.qq.com")
public interface IWeChatWebDAO {
    /**
     * 通过code获取openId
     *
     * @param appId     公众号的唯一标识
     * @param secret    公众号的appsecret
     * @param code      认证返回的code
     * @param grantType 填写为authorization_code
     * @return WeChatOpenIdRspDTO
     */
    @RequestMappingEx(name = "sns/oauth2/access_token", method = HttpMethod.GET)
    WeChatOpenIdRspDTO getOpenId(@RequestParam(value = "appid") String appId,
                                 @RequestParam(value = "secret") String secret,
                                 @RequestParam(value = "code") String code,
                                 @RequestParam(value = "grant_type") String grantType);

    /**
     * 通过refresh_token刷新access_token
     *
     * @param appId        公众号的唯一标识
     * @param grantType    填写为refresh_token
     * @param refreshToken 填写通过access_token获取到的refresh_token参数
     * @return
     */
    @RequestMappingEx(name = "sns/oauth2/refresh_token", method = HttpMethod.GET)
    WeChatOpenIdRspDTO getAccessToken(@RequestParam(value = "appid") String appId,
                                      @RequestParam(value = "grant_type") String grantType,
                                      @RequestParam(value = "refresh_token") String refreshToken);

    /**
     * 检验授权凭证（access_token）是否有效
     *
     * @param accessToken 网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同
     * @param openId      用户的唯一标识
     * @return WeChatErrorRspDTO
     */
    @RequestMappingEx(name = "sns/auth", method = HttpMethod.GET)
    WeChatErrorRspDTO checkAccessToken(@RequestParam(value = "access_token") String accessToken,
                                       @RequestParam(value = "openid") String openId);

    /**
     * 拉取用户信息(网页授权需scope为 snsapi_userinfo)
     *
     * @param accessToken 网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同
     * @param openId      用户的唯一标识
     * @param lang        返回国家地区语言版本，zh_CN 简体，zh_TW 繁体，en 英语
     * @return WeChatUserInfoRspDTO
     */
    @RequestMappingEx(name = "sns/userinfo", method = HttpMethod.GET)
    WeChatUserInfoRspDTO getUserInfo(@RequestParam(value = "access_token") String accessToken,
                                     @RequestParam(value = "openid") String openId,
                                     @RequestParam(value = "lang") String lang);

}
